package test;
/**
 *折半查找://前提是数组本身是个有序的升序排列.
 *指定一个数,在一个数组中查找,指一个左边的下标,和一个右边的下标,中间一个指标用于指定中间一个数,通过中间一个数与指定的数进行比较,
 */
public class BirarySearch {
	public static void main(String[] args){
		int[] arr={1,2,3,4,5,6,7,8,9};
		int index=binarySearch(arr,7);
		System.out.println(index);
	}
public static int binarySearch(int[] arr,int target){
	int low=0;
	int high=arr.length-1;
	while(low<=high){
			int mid=(low+high)/2;
			if(arr[mid]>target){
				high=mid-1;
				continue;
			}else if(arr[mid]<target){
			low=mid+1;
			continue;
			}else{
				return mid;
		     }	
	 }
		return -1;
 }
}
